<?php

namespace App\Http\Controllers\Api\V1\Requests;

use App\Supports\Sms\Sms;
use Closure;
use App\Http\Controllers\Api\V1\Requests\FormRequest;

class BankCardRequest extends FormRequest

{

    public function rules()
    {
        return [
        	'method' => ['required','string'],
        	'sms_key' => 'required|string',
        	'is_default' =>'required|boolean',
			'sms_code' => ['required', 'string', $this->smsCode()],
        ];
    }

   public function smsCode(): Closure
	{
		return function ($attribute, $value, $fail) {
			$sms = app(Sms::class);
			$verification = $sms->isSmsDataRight(request('sms_key'),$this->user()->phone, $value);
			
			$key = "bind_user_pay_method_".$this->user()->phone;

			if($sms->isFailCountOver($key)){

				return $fail('验证码申请过于频繁，请稍后再试');
			}

			if (! $verification) {
				$sms->incrFailCount($key);	
				return $fail('短信验证码无效');

			}
			return true;
		};
	}
}
